/**
 * Package:com.afteryuan.util.code;
 * $Id$
 * Copyright(c) 2001-2009 www.afteryuan.com
 */
package com.afteryuan.util.code;

import com.afteryuan.util.FileUtil;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * ServiceGenerator
 * <p/>
 * <p><a href="ServiceGenerator.java.html"><i>View Source</i></a></p>
 *
 * @author <a href="mailto:afteryuan@gmail.com">$Author$</a>
 * @version $Revision$
 */
public class ServiceGenerator extends AbstractorGenerator {
    public static final String SERVICE_SINGLE_PACKAGENAME="service";
    public static final String CLASS_STUFFIX_NAME="Service";
    protected File serviceFile;
    protected String servicePackageName;
    protected String serviceClassName;
    protected String serviceFQClassName;

    /**
     * 构造器
     *
     * @param model   领域模型文件
     * @param srcRoot 源代码的根目录
     */
    protected ServiceGenerator(File model, File srcRoot) {
        super(model, srcRoot);
        serviceClassName = modelClassName+CLASS_STUFFIX_NAME;
        serviceFile = new File(model.getParent().replaceAll(MODE_SINGLE_PACKAGENAME,SERVICE_SINGLE_PACKAGENAME), serviceClassName + ".java");
        servicePackageName = modelPackageName.replaceAll(MODE_SINGLE_PACKAGENAME,SERVICE_SINGLE_PACKAGENAME);
        serviceFQClassName = servicePackageName+"."+serviceClassName;
    }

    public void generate() throws IOException {
        if(serviceFile.exists()&&!isReplace)return;
        FileUtil.createFile(serviceFile.getPath());
        FileWriter fw = new FileWriter(serviceFile);
        fw.write("/**\n" +
                " * Package:" + servicePackageName + ";\n" +
                " * $Id$\n" +
                " * Copyright(c) 2001-2005 www.afteryuan.com\n" +
                " */\n");

        fw.write("package " + servicePackageName + ";\n\n");
        fw.write("import " + modelFQClassName + ";\n");
        fw.write("import " + modelRootPackageName + "." + SERVICE_SINGLE_PACKAGENAME + ".Service;\n");

        fw.write("\n");
        fw.write("/**\n" +
                " * Service\n" +
                " * <p/>\n" +
                " * <p><a href=\"" + serviceClassName + ".java.html\"><i>View Source</i></a></p>\n" +
                " *\n" +
                " * @author <a href=\"mailto:afteryuan@gmail.com\">$" + "Author" + "$</a>\n" +
                " * @version $" + "Revision" + "$\n" +
                " */\n");
        fw.write("public interface " + serviceClassName + " extends Service<" + modelClassName + "> {\n" +
                "\n" +
                "}");
        fw.close();
        System.out.println("generate file:"+serviceFile);
    }
}
